package hk.edu.polyu.comp.motioncapture.point;

import hk.edu.polyu.comp.motioncapture.collection.Sort;

public class Point implements Comparable<Point> {
	public static final int COMPARE_BY_X = 0;
	public static final int COMPARE_BY_Y = 1;

	protected static int compareBy;
	protected static boolean sortAsc;

	protected double x;
	protected double y;

	public int compareTo(Point pt) {
		int result = 0;

		switch (compareBy) {
		case COMPARE_BY_Y:
			result = ((Double) y).compareTo(pt.y);
			break;
		case COMPARE_BY_X:
		default:
			result = ((Double) x).compareTo(pt.x);
			break;
		}

		if (!sortAsc) {
			result = -result;
		}
		return result;
	}

	public Point add(Point pt) {
		return new Point(x + pt.getX(), y + pt.getY());
	}

	public static Point[] sortPointByY(Point[] pts) {
		setCompareBy(Point.COMPARE_BY_Y);
		setSortAsc(true);

		return Sort.heapSort(pts);
	}

	public static Point[] sortPointByX(Point[] pts) {
		setCompareBy(Point.COMPARE_BY_X);
		setSortAsc(true);

		return Sort.heapSort(pts);
	}

	public double distanceTo(Point pt) {
		return Math.sqrt((x - pt.x) * (x - pt.x) + (y - pt.y) * (y - pt.y));
	}

	public Point(double x, double y) {
		this.x = x;
		this.y = y;

		compareBy = COMPARE_BY_X;
		sortAsc = true;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public void setX(double x) {
		this.x = x;
	}

	public void setY(double y) {
		this.y = y;
	}

	public String toString() {
		return String.format("(%.2f,%.2f)", x, y);
	}

	public static void setCompareBy(int compare) {
		compareBy = compare;
	}

	public static void setSortAsc(boolean sort) {
		sortAsc = sort;
	}
}
